<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PAX 用户登录</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header text-white" style="background-color: #417690;">
                        <h3 class="text-center">PAX 用户登录</h3>
                    </div>
                    <div class="card-body">
                        <div id="error-message" class="alert alert-danger d-none"></div>
                        <div id="success-message" class="alert alert-success d-none"></div>
                        
                        <form id="loginForm">
                            {% csrf_token %}
                            <div class="mb-3">
                                <label for="username" class="form-label">用户名</label>
                                <input type="text" class="form-control" id="username" name="username" required>
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label">密码</label>
                                <input type="password" class="form-control" id="password" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-primary w-100" style="background-color: #417690; border-color: #417690;">登录</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加JavaScript处理表单提交 -->
    <script>
        document.querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const username = document.querySelector('input[name="username"]').value;
            const password = document.querySelector('input[name="password"]').value;
        
            const response = await fetch('', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: new URLSearchParams({
                    username,
                    password
                })
            });
        
            const data = await response.json();
            if (data.success) {
                // 存储 Token 到 localStorage
                localStorage.setItem('pax_token', data.token);
                
                // 跳转到 pax_index
                window.location.href = data.redirect_url;
            } else {
                alert(data.error || 'Login failed');
            }
        });
    </script>
</body>
</html>